FILE(GLOB TARGETS_SRC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/*.cs)
LIST(REMOVE_ITEM TARGETS_SRC Load.cs)
IF (NOT USE_SVMLIGHT)
	LIST(REMOVE_ITEM TARGETS_SRC regression_svrlight_modular.cs)
ENDIF()

SET(CSHARP_FLAGS "${CMAKE_CURRENT_SOURCE_DIR}/Load.cs;/lib:${CSHARP_MODULAR_BUILD_DIR};/r:modshogun")

FOREACH(EXAMPLE_SRC ${TARGETS_SRC})
	STRING(REGEX REPLACE "(.*).cs" "csharp-\\1" EXAMPLE_NAME ${EXAMPLE_SRC})
	STRING(REGEX REPLACE "(.*).cs" "\\1.exe" EXAMPLE_BINARY ${EXAMPLE_SRC})

	ADD_CUSTOM_COMMAND(OUTPUT ${EXAMPLE_NAME}
		COMMAND	${CSHARP_COMPILER} ${CMAKE_CURRENT_SOURCE_DIR}/${EXAMPLE_SRC} ${CSHARP_FLAGS} -out:${EXAMPLE_BINARY}
		DEPENDS csharp_modular
		WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
	LIST(APPEND CSHARP_EXAMPLES ${EXAMPLE_NAME})

	add_test(NAME ${EXAMPLE_NAME}
		WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
		COMMAND ${CSHARP_INTERPRETER} ${CMAKE_CURRENT_BINARY_DIR}/${EXAMPLE_BINARY})
	set_property(TEST ${EXAMPLE_NAME} PROPERTY
		ENVIRONMENT "MONO_PATH=${CSHARP_MODULAR_BUILD_DIR}")
ENDFOREACH()

add_custom_target(build_csharp_examples ALL
				 DEPENDS ${CSHARP_EXAMPLES}
				 COMMENT "C# examples")
